package com.inspur.dgov.bsp.system.wrapper;

import cn.hutool.core.bean.BeanUtil;
import com.inspur.dgov.bsp.system.entity.PubConfig;
import com.inspur.dgov.bsp.system.entity.PubDict;
import com.inspur.dgov.bsp.system.service.PubConfigService;
import com.inspur.dgov.bsp.system.service.PubDictService;
import com.inspur.dgov.bsp.system.vo.DictVO;
import com.inspur.dgov.common.boot.mapper.BaseEntityWrapper;
import com.inspur.dgov.common.tool.node.TreeNodeUtil;
import com.inspur.dgov.common.tool.utils.SpringContextHolder;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典包装类
 *
 * @Author chiyupei
 * @Date 20/3/20 下午11:19
 * @Version 1.0
 * @Description 字典包装类,返回视图层所需的字段
 */
public class PubDictWrapper extends BaseEntityWrapper<PubDict, DictVO> {

    private static PubConfigService pubConfigService;

    private static PubDictService pubDictService;

    static {
        pubConfigService = SpringContextHolder.getBean(PubConfigService.class);
        pubDictService = SpringContextHolder.getBean(PubDictService.class);
    }

    public static PubDictWrapper build() {
        return new PubDictWrapper();
    }

    @Override
    public DictVO entityVO(PubDict dict) {
        DictVO dictVO = new DictVO();
        BeanUtil.copyProperties(dict, dictVO);
        if (null == dict.getParentId()) {
            dictVO.setParentName("");
        } else {
            PubDict parent = pubDictService.getById(dict.getParentId());
            dictVO.setParentName(parent.getName());
        }
        PubConfig config = pubConfigService.getById(dict.getConfigId());
        dictVO.setConfigName(config.getName());
        return dictVO;
    }

    public List<DictVO> listVO(List<PubDict> list) {
        List<DictVO> collect = list.stream().map(this::entityVO).collect(Collectors.toList());
        return TreeNodeUtil.merge(collect);
    }
}
